﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Rendering;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.DataStructures;
using DcsGauges.Gui.Gauges.Trigger;
using Microsoft.Xna.Framework.Graphics;
using DcsGauges.Gui.Gauges.ValueConverters;
using DcsGauges.Shared.DataStructures;
using Microsoft.Xna.Framework;
using DcsGauges.Shared.Exceptions;

namespace DcsGauges.Gui.Gauges.Panels
{
	class ElectricalPanel : GuiRenderableBase, IMultiTriggerGauge
	{
		#region Fields / Properties

		private TwoStateToggleSwitchGauge               apuGeneratePower                    = null;
		private TwoStateToggleSwitchGauge               inverterStandby                     = null;
		private TwoStateToggleSwitchGauge               emergencyFlood                      = null;
		private TwoStateToggleSwitchGauge               acGeneratorLeft                     = null;
		private TwoStateToggleSwitchGauge               acGeneratorRight                    = null;
		private TwoStateToggleSwitchGauge               batteryPower                        = null;
        private Texture2D                               backgroundTexture                   = null;

		#endregion


		#region Constructors

		public ElectricalPanel(GuiRenderableBase parent, URect area) : base(parent, area)
		{
            this.backgroundTexture                                      = TextureManager.GetTexture("Textures/Gauges/Backgrounds/ElectricalPanel");
			Texture2D state0Texture                                     = TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchDown");
			Texture2D state1Texture                                     = TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchUp");
			StateConverter<TwoStateToggleSwitchGauge.SwitchStates> conv = new TwoStateToggleSwitchConverter();

            float sw                = 0.15f; //found by runtime measurement in ui
            float sh                = 0.15f;

            float r1y               = 0.25f;
            float r2y               = 0.71f;

			this.apuGeneratePower   = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.2f,  r1y), new USize(sw, sh)), DcsProperties.ElectricalPanel.ApuGenerator,           state0Texture, state1Texture, conv);
			this.inverterStandby    = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.5f,  r1y), new USize(sw, sh)), DcsProperties.ElectricalPanel.Inverter,               state0Texture, state1Texture, conv);
			this.emergencyFlood     = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.83f, r1y), new USize(sw, sh)), DcsProperties.ElectricalPanel.EmergencyFlood,         state0Texture, state1Texture, conv);
			this.acGeneratorLeft    = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.2f,  r2y), new USize(sw, sh)), DcsProperties.ElectricalPanel.AcPowerGen.Left,        state0Texture, state1Texture, conv);
			this.acGeneratorRight   = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.5f,  r2y), new USize(sw, sh)), DcsProperties.ElectricalPanel.AcPowerGen.Right,       state0Texture, state1Texture, conv);
			this.batteryPower       = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.83f, r2y), new USize(sw, sh)), DcsProperties.ElectricalPanel.BatteryPower,           state0Texture, state1Texture, conv);

            this.apuGeneratePower.TriggerEvent      += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
            this.inverterStandby.TriggerEvent       += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
            this.emergencyFlood.TriggerEvent        += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
            this.acGeneratorLeft.TriggerEvent       += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
            this.acGeneratorRight.TriggerEvent      += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
            this.batteryPower.TriggerEvent          += new EventHandler<TriggerValueEventArgs>(OnChildGaugeHandleTriggerEvent);
		}

		#endregion


		#region Interface Implemenation

        #region Operations

        public Shared.DataStructures.DcsProperty[]  Properties
		{
			get 
			{ 
				return new DcsProperty[] 
				{
					this.apuGeneratePower.DataProperty,
					this.inverterStandby.DataProperty,
					this.emergencyFlood.DataProperty,
					this.acGeneratorLeft.DataProperty,
					this.acGeneratorRight.DataProperty,
					this.batteryPower.DataProperty,
				};
			}
		}

		public void  SetValue(Shared.DataStructures.DcsProperty property, float value)
		{
			if      (property == this.apuGeneratePower.DataProperty)        this.apuGeneratePower.Value         = value;
			else if (property == this.inverterStandby.DataProperty)         this.inverterStandby.Value          = value;
			else if (property == this.emergencyFlood.DataProperty)          this.emergencyFlood.Value           = value;
			else if (property == this.acGeneratorLeft.DataProperty)         this.acGeneratorLeft.Value          = value;
			else if (property == this.acGeneratorRight.DataProperty)        this.acGeneratorRight.Value         = value;
			else if (property == this.batteryPower.DataProperty)            this.batteryPower.Value             = value;
			else
			{
				throw new DcsArgumentException(string.Format(Localization.ExcPropertyInvalidForGauge2A, property.Name, this.GetType().Name));
			}
		}

		public float  GetValue(Shared.DataStructures.DcsProperty property)
		{
			throw new NotImplementedException();
		}

        #endregion

        #region Events
        
        public event EventHandler<TriggerValueEventArgs>  TriggerEvent;

        #endregion

		#endregion


		#region Overrides

		public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Rectangle parentRect, float globalZIndex)
		{
            Rectangle viewportRect                  = this.CalcViewportRect(parentRect, this.backgroundTexture.Bounds);
            spriteBatch.Draw(this.backgroundTexture, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, this.NextFrontIndex(globalZIndex, 1));

			this.children.Render(spriteBatch, device, viewportRect, globalZIndex);
		}

		#endregion  


        #region Event Handlers

        private void OnChildGaugeHandleTriggerEvent(object sender, TriggerValueEventArgs e)
        {
            this.FireTriggerEvent(sender, e);
        }

        #endregion


        #region Helpers

        private void FireTriggerEvent(object sender, TriggerValueEventArgs e)
        {
            if (this.TriggerEvent != null)
            {
                this.TriggerEvent(sender, e);
            }
        }

        #endregion
    }
}
